Assignment Keras Intro

For this assignment, we will examine our keras MNIST network, and try to optimize it. We will interface keras to scikit learn to do this, following the strategy outlined in the book.

For the extra credit, I want you to learn how to implement a simple model using the Keras Functional API.

Task 1: Get the data

Get both the test and train data from Keras.

As we did in the in-class work, use the "Train" sample to form a train and validation dataset. Keep the Test set separate and use it for final performance evaluation after training.

Task 2: Modify "build_model" function

Use the version from the in-class workbook as a starting point.

However, you will want to add arguments for the following:

Task 3: Use Scikit To Do a Randomized Search of Model Paremeters

Follow the strategy on p.320-321 of the text. You will use a KerasClassifier instead of a KerasRegressor used in the text, but everything else is basically the same.

You will want to look at the following paremeters:

This will only take about 3-5 minutes.

Task 4: Same as Task 3, but more Paremeters

Follow the strategy on p.321 of the text; You will want to look at the following paremeters:

This will take about 30 minutes to do the training!

Task 5: Print parameters and score of the best model from Task 4

See top of page 322.

Task 6: Retrain model using best parameters, and then test it using the test data

You should provide:

Extra Credit: Build the best model using the Functional API

We have used the "Sequential" Keras API to build the models we have used so far. This is the best approach to use when our models are simple, but if we want to build a complex model such as:

then we need to use the more flexible "Functional" Keras API.

The book has a short descriptiuon of how to do this on p. 308-312. You can also find a simple writeup of this [here] (https://towardsdatascience.com/3-ways-to-create-a-machine-learning-model-with-keras-and-tensorflow-2-0-de09323af4d3).

For the extra credit, I want you to implement the best model above using the functional API. Train it, and verify you get the same (or at least similar) performance on the test set. For the perfomrance you can just call model.evaluate (no plots needed for this).

For the sequential model, I got a test accuracy of 0.969

For the Functional model, I got a test accuracy of 0.974